package demo1;

import java.util.Stack;

/**
 * @Author liangzai
 * @Description:
 */
public class MinStack {

    Stack<Integer> stack;
    Stack<Integer> minstack;

    public MinStack() {
        stack = new Stack<>();
        minstack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if(minstack.isEmpty()){
            minstack.push(val);
        }else{
            if (val > minstack.peek()) {
                minstack.push(minstack.peek());
            }else {
                minstack.push(val);
            }
        }
    }

    public void pop() {
        int val = stack.pop();
        if (val == minstack.peek()) {
            minstack.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minstack.peek();
    }
}
